<template>
    <Card>
        <Form ref="columnJson" :model="columnJson">
            <FormItem>
                <Row :gutter="16">
                    <Col span="2">&nbsp;</Col>
                    <Col span="5">
                        <Input v-model="modalName" placeholder="example: user" @on-keyup="handleSearchTable">
                            <Dropdown slot="append" trigger="click">
                                <a href="javascript:void(0)">
                                    <Icon type="umbrella" size="20">&nbsp;</Icon>
                                </a>
                                <DropdownMenu slot="list" v-for="(item, index) in models" :key="index">
                                    <DropdownItem divided style="text-align:left;">
                                        <Button type="warning" @click="handleQuery(item.tablename,item.comments)" style="padding:0px 15px;">
                                            <Icon type="load-b" size="15"></Icon>
                                            <span style="font-size:15px;">{{index+1}} : {{item.tablename}} - {{item.comments}}</span>
                                        </Button>
                                    </DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                        </Input>
                    </Col>
                    <Col span="5">
                        <Input v-model="modalCName" placeholder="例如: 用户"></Input>
                    </Col>
                    <Col span="10">
                        <Button type="primary" icon="ios-cog" @click="handleSubmit()">生成</Button>
                        <!--<Button type="primary" icon="ios-cog" @click="handleSubmit('uap30')">提交(UAP3.0)</Button>-->
                        <!--<Button type="success" icon="ios-cog" @click="handleSubmit('uap316')">提交(UAP3.1.6)</Button>-->
                        <!--<Button type="warning" icon="plus-round" @click="handleAdd">增加</Button>-->
                        <!--<Button type="error" icon="android-delete">删除</Button>-->
                    </Col>
                    <Col span="2">&nbsp;</Col>
                </Row>
            </FormItem>

        </Form>

        <Input v-model="addCode" type="textarea" :autosize="{minRows: 5,maxRows: 15}" placeholder="代码生成到这里"></Input>
    </Card>
</template>

<script>
const {dialog} = require('electron').remote
const {ipcRenderer} = require('electron')
export default {
    name: 'servercode',
    data () {
        return {
            modalName: '',
            modalCName: '',
            keywords:'',
            columnJson: [
                {columnName:'id',dataType:'int',columnLen:'11',constraintName:'PRIMARY KEY',commentName:'主键'}
                ,{columnName:'',dataType:'',columnLen:'',constraintName:'',commentName:''}
                ,{columnName:'',dataType:'',columnLen:'',constraintName:'',commentName:''}
            ],
            columnJsonBak:[
                {columnName:'id',dataType:'int',columnLen:'11',constraintName:'PRIMARY KEY',commentName:'主键'}
                ,{columnName:'',dataType:'',columnLen:'',constraintName:'',commentName:''}
                ,{columnName:'',dataType:'',columnLen:'',constraintName:'',commentName:''}
            ],
            models: [],
            modelsBak: [],
            addCode: ''
        };
    },
    methods: {
        handleSubmit (typename) {
            let self = this;

            function getFirstUpper(word){
                if(word){
                    var first_char = word.substring(0,1);
                    var last_char = word.substring(1);
                    return first_char.toUpperCase() + last_char;
                }
            }

            function getLowerTF(col,flag){
                if(col && col.indexOf("_")!=-1){
                    var new_col = "";
                    var arr = col.split("_");
                    new_col = arr[0];
                    if(flag){
                        new_col = getFirstUpper(arr[0]);
                    }
                    for(var i=1;i<arr.length;i++){
                        new_col += getFirstUpper(arr[i]);
                    }
                    return new_col;
                }else{
                    return col;
                }
            }

            var insert_str = "";
            var values_str = "";
            var flag = "";
            for(var i=0;i<self.columnJson.length;i++){
                insert_str += flag + self.columnJson[i].columnName;
                values_str += flag + "#{"+ getLowerTF(self.columnJson[i].columnName)+"}";
                flag = ", ";
            }

            var newcode = '@Insert({"insert into '+ self.modalName +'('+insert_str+') values('+values_str+')"})\r\n' +
            '@Options(useGeneratedKeys = true, keyProperty = "'+getLowerTF(self.columnJson[0].columnName)+'" ,keyColumn="'+self.columnJson[0].columnName+'")\r\n' +
            'int adds('+getLowerTF(self.modalName,true)+' '+getLowerTF(self.modalName)+');\r\n'

            self.addCode = newcode;

        },
        handleQuery (modalName,comments) {
            this.modalName = modalName
            this.modalCName = comments
            if(this.modalCName && this.modalCName.substr(this.modalCName.length-1)=='表'){
                this.modalCName = this.modalCName.substr(0,this.modalCName.length-1);
            }
            ipcRenderer.send('sendcontent',modalName)
        },
        handleSearchTable () {
            let keywords = this.modalName;
            let models = this.modelsBak;
            let new_models = [];
            for(let i=0;i<models.length;i++){
                let tablename = models[i].tablename?models[i].tablename:'';
                let comments = models[i].comments?models[i].comments:'';
                let concat_str = tablename + comments + (i+1);
                if(concat_str.indexOf(keywords) != -1 && new_models.length<10){
                    new_models.push(models[i]);
                }
            }
            this.models = new_models;
        }
    },
    mounted () {

        let projectName = localStorage.getItem('projectname');
        if("ma-" == projectName.substr(0,3)){
            this.moduleTypes = ['web','service']
        }

        //清除全部消息监听
        ipcRenderer.removeAllListeners('tips', 'receivelist', 'receivecontent')
        //接收消息
        ipcRenderer.on('tips', (event, person) => {
            this.$Message.success({content: person, duration: 2})
        })
        //接收模块列表数据
        ipcRenderer.on('receivelist', (event, person) => {
            this.modelsBak = JSON.parse(person)
            this.models = [];
            let thesize = 10;
            if(!this.modelsBak || this.modelsBak.length==0) {
                thesize=0;
            }
            if(this.modelsBak && this.modelsBak.length<10) thesize=this.modelsBak.length;
            for(let i=0;i<thesize;i++){
                this.models.push(this.modelsBak[i]);
            }
        })
        //接收模块内容数据
        ipcRenderer.on('receivecontent', (event, person) => {
            this.columnJson = JSON.parse(person)
            this.columnJsonBak = this.columnJson
            this.keywords = ''
        })

        //初始化数据
        ipcRenderer.send('init',localStorage.getItem('mysqlhost')+','+localStorage.getItem('mysqluser')+','+localStorage.getItem('mysqlpassword')+','+localStorage.getItem('mysqlport')+','+localStorage.getItem('mysqldatabase')+','+localStorage.getItem('clientcodelist')+','+localStorage.getItem('servercodelist'))
    }

};
</script>
